const http=require('http');
const fs=require('fs');

http
    .createServer((req,res)=>{
        if(req.url=='/'){
            fs.createReadStream('./pic.html').pipe(res);
        }
        else if(req.url=='/upload'){
            let img=[];
            let size=0;
            req.on('data',chunk=>{
                console.log(chunk,'----')
                img.push(chunk);
                size += chunk.length;
            })
            req.on('end',function(){
                const buffer=Buffer.concat(img,size);
                fs.writeFileSync('samll.jpg', buffer, (err)=>{
                    if(err){
                        return console.error(err);
                    }
                    console.log('文件上传成功');
                });
                res.end('文件上传成功');   
            })
        }
    })
    .listen(1314);